package android.javaClasses;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import java.util.ArrayList;
import android.util.Log;

import javax.activation.DataHandler;

/**
 * Created by IntelliJ IDEA.
 * User: michelle
 * Date: 4/18/11
 * Time: 10:51 AM
 * To change this template use File | Settings | File Templates.
 */


public class Client {
    //private String ip ="10.0.2.2"; //ip-address to emulator. Needs to be changed for non-emulator use
    private int port = 12345;
    private Stream stream;
    private Handler mHandler;

    public Client(String ip, Handler mHandler) {
        this.mHandler = mHandler;
        stream = new Stream(ip, port);
        stream.setOnCloseStreamListener(new Stream.OnCloseStreamListener() {
            @Override
            public void onCloseStream() {
                //Stream is closed automatically, don't need to call close()
                //Do something
            }
        });


        stream.setOnReadObjectListener(new Stream.OnReadObjectListener() {
            @Override
            public void onReadObject(Content content) {
                actOnObject(content);
//                Log.i("ReadObjectFromServer", content.getText());
                //Do something with object, insert a switch
                /*if(obj.isDoingSomethingBad()) {
                    stream.close();
                } else if(obj.isDoingGoodStuff()) {
                    stream.writeObject(new GoodStuff());
                }  */
            }
        });
    }


    public void startReadFromStream() {
        stream.startReading();
    }

    public void closeStream() {
        stream.close();
    }

    /**
     * Writes object type Content to the stream
     * @param content
     */
    public void writeToStream(Content content) {
        stream.writeObject(content);
    }


    private void actOnObject(Content content) {
        Message msg = Message.obtain();
        Bundle bundle = new Bundle();
        int id = content.getTypeOfContent();
        switch (id) {
            case Content.SEND_CHOSEN_CELL:
                msg.what = Content.SEND_CHOSEN_CELL;
                float yValue = content.getyValue();
                float xValue = content.getxValue();
                bundle.putFloat("xValue", xValue);
                bundle.putFloat("yValue", yValue);
                msg.setData(bundle);
                mHandler.sendMessage(msg);
                break;

            case Content.GAME_FINISHED:
                ArrayList<String> players = content.getPlayers();
                String winner = content.getWinner();
                boolean finished = content.isFinished();
                bundle.putStringArrayList("players", players);
                bundle.putString("winner", winner);
                bundle.putBoolean("finished", finished);
                msg.setData(bundle);
                mHandler.sendMessage(msg);
                break;

            case Content.START_STATUS:
                int numPlayers = content.getNumPlayers();
                int typeOfGame = content.getTypeOfGame();
                bundle.putInt("numPlayers", numPlayers);
                bundle.putInt("typeOfGame", typeOfGame);
                msg.setData(bundle);
                mHandler.sendMessage(msg);
                break;
        }

    }


}